package back_0413;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

/**
 * Auth: bruce-sha
 * Date: 2015/4/5
 */
public class Dates {


//    public static int sub() {
//        DateTime start = new DateTime();
//
//        DateTime end = new DateTime();
//
//        java.time.Period p = new java.time.Period(start, end, PeriodType.days()); // 最后一个参数如果不写的话，下面的返回值将会是错误的。
//
//        int day = p.getDays();
//
//        return day;
//    }

    //    http://stackoverflow.com/questions/25747499/java-8-calculate-difference-between-two-localdatetime
    public static int java8() {
        LocalDateTime begin2 = LocalDateTime.of(2014, 11, 4, 0, 0, 0, 0);

        System.out.println(ChronoUnit.DAYS.between(begin2, LocalDateTime.now()));

        //表示两个瞬时时间的时间段
        Duration d1 = Duration.between(Instant.ofEpochMilli(System.currentTimeMillis() - 12323123), Instant.now());
//得到相应的时差
        System.out.println(d1.toDays());
        System.out.println(d1.toHours());
        System.out.println(d1.toMinutes());
        System.out.println(d1.toMillis());
        System.out.println(d1.toNanos());


//        begin2.toEpochSecond()


        Instant begin = Instant.ofEpochMilli(11);

        Instant from = Instant.from(begin);
        Duration d = Duration.between(from, Instant.now());
        return (int) d.toDays();

//        OffsetDateTime dd=OffsetDateTime.of();
//        dd.getd
//
//        LocalDate begin = LocalDate.of(2014, 11, 4);
//        LocalDate now = LocalDate.now();
//
//        java.time.Period p = java.time.Period.between(begin, now);
//
////        p.minusDays(1)
//
//        return p.getDays();
    }


    /**
     * @param args
     * @throws java.text.ParseException
     */
    public static void main(String[] args) throws ParseException {

        System.out.println(java8());

//        // TODO Auto-generated method stub
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date d1 = sdf.parse("2012-09-08 10:10:10");
//        Date d2 = sdf.parse("2012-09-15 00:00:00");
//        System.out.println(daysBetween(d1, d2));
//
//        System.out.println(daysBetween("2012-09-08 10:10:10", "2012-09-15 00:00:00"));
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws java.text.ParseException
     */
    public static int daysBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();

        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 字符串的日期格式的计算
     */
    public static int daysBetween(String smdate, String bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(smdate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(bdate));
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }

}
